﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace THO7AlgoritmTimerApplication.Filters
{
    public class Binair : Filter
    {
        public Binair()
            : base()
        {
        }

        public override Bitmap RunFilter()
        {
            Bitmap binairImage = new Bitmap(_width, _height);
            BitmapData binairData = binairImage.LockBits
                (new Rectangle(0, 0, _width, _height),
                System.Drawing.Imaging.ImageLockMode.WriteOnly,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Bitmap detectImage = new Bitmap(_width, _height);
            BitmapData detectData = detectImage.LockBits(new Rectangle(0, 0, _width, _height),
                System.Drawing.Imaging.ImageLockMode.WriteOnly,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            unsafe
            {
                byte* sourcePointer = (byte*)_sourceData.Scan0;
                byte* binairPointer = (byte*)binairData.Scan0;
                int total = _sourceData.Stride * _height;

                for (int i = 0; i < total; i += 3)
                {
                    if (sourcePointer[i] < 60 && sourcePointer[i + 1] > 120 && sourcePointer[i + 2] > 190)
                    { //wit maken
                        binairPointer[i] = 255;
                        binairPointer[i + 1] = 255;
                        binairPointer[i + 2] = 255;
                    }
                    else
                    { //zwart maken
                        binairPointer[i] = 0;
                        binairPointer[i + 1] = 0;
                        binairPointer[i + 2] = 0;
                    }
                }
                _sourceImage.UnlockBits(_sourceData);


                //controleren op dingen
                byte* detectPointer = (byte*)detectData.Scan0;

                int eerste_x = 0;
                int laatste_x = 0;
                bool eerst = true;
                for (int z = 0; z < total; z += 3)
                {
                    if (binairPointer[z] == 255)
                    {
                        int test = 0;
                        for (int j = 0; j < 3 * 40; j += 3)
                        {
                            if (binairPointer[z - j] == 255)
                            {
                                test++;
                            }
                        }
                        if (test > 30)
                        {
                            detectPointer[z] = 0;
                            detectPointer[z + 1] = 255;
                            detectPointer[z + 2] = 0;
                            laatste_x = z;

                        }
                    }

                }



                for (int i = 0; i < total; i += 3)
                {
                    if (binairPointer[i] == 255)
                    {
                        int test = 0;
                        for (int j = 0; j < 3 * 40; j += 3)
                        {
                            if (binairPointer[i + j] == 255)
                            {
                                test++;
                            }
                        }
                        if (test > 30)
                        {
                            detectPointer[i] = 0;
                            detectPointer[i + 1] = 255;
                            detectPointer[i + 2] = 0;
                            if (eerst)
                            {
                                eerste_x = i;
                                eerst = false;
                            }
                        }
                    }

                }

                Point p1 = new Point(eerste_x % binairData.Stride, eerste_x / binairData.Stride);
                Point p2 = new Point(laatste_x % binairData.Stride, laatste_x / binairData.Stride);
                binairImage.UnlockBits(binairData);

                int w = p2.X - p1.X;
                int h = p2.Y - p1.Y;





                return this.drawLines(eerste_x, laatste_x, w, h, _sourceImage);
                //row * 4 + col;
                //terug berekenen
                /*
                int p1_1d = p1.Y * outputData.Stride + p1.X;
                int p2_1d = p2.Y * outputData.Stride + p2.X;
                */


                /*
                Size s = new Size(p2.X, p2.Y);
                Rectangle r = new Rectangle(p1, s);
                Bitmap image = CropImage(c, r);

                Console.WriteLine(p1.X + " " + p1.Y + " " + p2.X + " " + p2.Y);

                //output.UnlockBits(outputData2);
                */

            }
        }



        private unsafe Bitmap drawLines(int eerste_x, int laatste_x, int w, int h, Bitmap copy)
        {
            Bitmap c = (Bitmap)copy.Clone();
            if (copy == null)
            {
                Console.WriteLine("is null???");

            }

            BitmapData correctData = c.LockBits(new Rectangle(0, 0, _width, _height),
            System.Drawing.Imaging.ImageLockMode.WriteOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);


            byte* correctPnt = (byte*)correctData.Scan0;

            // Horizontaal boven & onder
            for (int i = 0; i <= w; i += 3)
            {
                //boven
                correctPnt[eerste_x + i] = 0;
                correctPnt[eerste_x + i + 1] = 0;
                correctPnt[eerste_x + i + 2] = 255;
                //onder
                correctPnt[laatste_x - i] = 0;
                correctPnt[laatste_x - i + 1] = 0;
                correctPnt[laatste_x - i + 2] = 255;
            }

            // Verticaal links & rechts
            for (int i = 1; i <= h; i++)//links
            {
                
                correctPnt[eerste_x + i * _sourceData.Stride] = 0;
                correctPnt[eerste_x + i * _sourceData.Stride + 1] = 0;
                correctPnt[eerste_x + i * _sourceData.Stride + 2] = 255;

            }

            for (int i = 0; i >= -h; i--)//rechts
            {
                
                correctPnt[laatste_x + (i * _sourceData.Stride)] = 0;
                correctPnt[laatste_x + (i * _sourceData.Stride) + 1] = 0;
                correctPnt[laatste_x + (i * _sourceData.Stride) + 2] = 255;

            }
            c.UnlockBits(correctData);
            return c;
        }

        public Bitmap CropImage(Bitmap source, Rectangle section)
        {

            // An empty bitmap which will hold the cropped image
            Bitmap bmp = new Bitmap(section.Width, section.Height);
            Graphics g = Graphics.FromImage(bmp);
            // Draw the given area (section) of the source image
            // at location 0,0 on the empty bitmap (bmp)
            g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

            return bmp;

        }
    }
}
